<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<template>
					  <el-select  @change="selectedShop" v-model="selectedShopId" placeholder="请选择要查询的店铺">
						<el-option
							v-for="(item,i) in shops"
							:key="i" :label="item.shopName" :value="item.shopId" >
							  {{item.shopName}}
						 </el-option>
					  </el-select>
					</template>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd1">新增分类</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table v-if="classifys!=''" :data="classifys" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="classifyName" label="分类名称" width="120" sortable>
			</el-table-column>

			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="分类名称" prop="goodsName">
					<el-input v-model="editForm.classifyName" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

<!--		&lt;!&ndash;新增界面&ndash;&gt;-->
<!--		<el-dialog title="新增商品" :visible.sync="addFormVisible" :close-on-click-modal="false">-->
<!--			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">-->
<!--				<el-form-item label="商品名称">-->
<!--					<el-input v-model="addForm.goodsName"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="商品价格">-->
<!--					<el-input v-model="addForm.goodsPrice"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="商品特色">-->
<!--					<el-input v-model="addForm.goodsCharacteristics"></el-input>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="商品特色">-->
<!--				<el-select @change="selectedClassify"  v-model="addForm.classifyId" placeholder="请选择分类">-->
<!--					<el-option-->
<!--						v-for="(item,i) in classifys"-->
<!--						:key="i" :label="item.classifyName" :value="item.classifyId" >-->

<!--					 </el-option>-->
<!--				  </el-select>-->
<!--				</el-form-item>-->
<!--			</el-form>-->
<!--			<div slot="footer" class="dialog-footer">-->
<!--				<el-button @click.native="addFormVisible = false">取消</el-button>-->
<!--				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>-->
<!--			</div>-->
<!--		</el-dialog>-->

		<el-dialog title="新增分类" :visible.sync="addFormVisible1" :close-on-click-modal="false">
			<el-form :model="addForm1" label-width="80px" ref="addForm1">
				<el-form-item label="分类名称">
					<el-input v-model="addForm1.classifyName"></el-input>
				</el-form-item>
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible1 = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit1" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
	import  qs from 'qs'
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				page: [],
				goods:[],
				shops:'',
				total: 0,
				pageNum: 1,
				file:'',
				classifys:'',
				selectedClassifyId:'',
				selectedShopId:'',
				listLoading: false,
				sels: [],//列表选中列
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					classifyName:''
				},

				addFormVisible: false,//新增界面是否显示
				addFormVisible1: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					goodsName: [
						{ required: true, message: '请输入商品名', trigger: 'blur' }
					],
					
					
				},
				//新增界面数据
				addForm: {
					goodsName: '',
					goodsPrice:'',
					goodsCharacteristics:'',
					classifyId:'',
					shopId:''
				},
				addForm1:{
					classifyName:'',
					shopId:''
				}

			}
		},
		methods: {
			getByClassify(data){
				this.goods=''
				let params = {
					classifyId:data
				}
				this.$axios({
					method:'get',
					params,
					url:'/merchant/getGoodsByClassifyId'
				}).then(r=>{
					if(r.data.err_code===0){
						this.goods = r.data.result
						this.$axios({
							method:'get',
							url:'/merchant/getClassifyById',
							params
						}).then(r=>{
							if(r.data.err_code===0){
								this.goods.forEach(item=>{
									item.goodsClassifyEntity = r.data.result
								})
							}
						})
					}else{
						this.$message.warning('当前分类还没有商品')
					}
				})
			},
			upload(e){
				var file = e.target.files[0]
				var index = file.name.lastIndexOf('.')
				var suffix = file.name.substring(index+1)
				if(suffix!='jpeg'&&suffix!='jpg'&&suffix!='png'){
					return this.$message.error('上传的不是图片')
				}
			},
			selectedClassify(data){
				this.addForm.classifyId=data
				this.addForm.shopId = this.selectedShopId
			},
			
			selectedShop(data){
				this.selectedShopId=data
				this.selectedClassifyId = ''
				this.goods=''
				this.classifys=''
				this.getClassify()
			},
			getClassify(){
				this.$axios({
					method:'get',
					params:{
						shopId:this.selectedShopId
					},
					url:'/merchant/getClassify'
				}).then(r=>{
					if(r.data.err_code===0){
						this.classifys = r.data.result
					}
				})
			},
			handleCurrentChange(val) {
				this.pageNum = val;
				this.getGoodsPage();
			},
			getShops(){
				this.$axios({	
					method:'get',
					params:{
						merchantId:this.$store.state.user.merchantId
					},
					url:'/merchant/getShop'
				}).then(r=>{
					this.shops = r.data.result
				}).catch(err=>{
					this.$error()
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('此操作将删除分类和该分类下的所有商品', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { classifyId: row.classifyId };
					this.$axios({
						method:'get',
						params:para,
						url:'/merchant/delClassify'
					}).then(r=>{
						if(r.data.err_code==0){
							this.$message.success('删除成功')
							this.getClassify()
							
						}else{
							this.$message.error('网络错误，请稍后重试')
						}
						this.listLoading = false
					}).catch(err=>{
						this.listLoading = false
						this.$message.error('网络错误，请稍后重试')
					})
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm  = row
				// this.editForm = Object.assign({}, row);
			},
			//判断是否已选店铺
			verifyShop(){
				if(this.selectedShopId==''){
					this.$message.warning('请先选择店铺')
					return false;
				}
			},
			//显示新增界面
			handleAdd1: function () {
				if(this.verifyShop()==false) return 
				this.addFormVisible1 = true;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let data = Object.assign({}, this.editForm)
							this.$axios({
								method:'post',
								data,url:'/merchant/updateClassifyName'
							}).then(r=>{
								if(r.data.err_code===0){
									this.$message.success('修改成功')
								}else{
									this.$message.error('修改失败')
								}
								this.getClassify()
								this.editLoading = false;
								this.editFormVisible = false
							}).catch(err=>{
								this.editLoading = false;
								this.editFormVisible = false
							})
						});
					}
				});
			},
			//新增分类
			addSubmit1: function () {
				this.$refs.addForm1.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							this.addForm1.shopId = this.selectedShopId
							let para = Object.assign({}, this.addForm1);
							this.$axios({
								method:'post',
								data:para,
								url:'/merchant/addClassify'
							}).then(r=>{
								if(r.data.err_code===0){
									this.$message.success('添加分类成功!')
									for(let i in this.addForm1){
										this.addForm1[i]=''
									}
									this.addFormVisible1 = false;
									this.getClassify()
								}else{
									this.$message.error('分类已存在！')
								}
								this.addLoading = false;
							}).catch(err=>{
								this.$error()
							})
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveUser(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getGoodsPage();
					});
				}).catch(() => {

				});
			}
		},
		created() {
			// this.getGoodsPage()
			this.getShops()
		}
	}

</script>

<style scoped>

</style>